/* Hybrid System Abstractor
 *
 * Copyright (c) 2013, Fortiss GmbH - An-Institut TU Muenchen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fortiss.automaton;

import javax.xml.bind.annotation.*;
import org.fortiss.util.*;
import java.util.*;

@XmlRootElement
public class HybridNode extends Node
{
    private Predicate invariant;
    private IntervalMatrix flow;
    
    @XmlElement
    private Set<AutonomousUpdateEdge> autonomousEdge;
    @XmlElement
    private Set<ControlledUpdateEdge> controlledEdge;
    
    public HybridNode() {}
    
    public void init(int id, String name, boolean init, Predicate invariant, IntervalMatrix flow)
    {
        super.init(id, name, init);
        this.invariant = invariant;
        this.flow = flow;
        this.autonomousEdge = new HashSet<AutonomousUpdateEdge>();
        this.controlledEdge = new HashSet<ControlledUpdateEdge>();
    }
    
    @Override
    public void addEdge(Edge e)
    {
        if (e instanceof ControlledUpdateEdge)
            controlledEdge.add((ControlledUpdateEdge) e);
        else if (e instanceof AutonomousUpdateEdge)
            autonomousEdge.add((AutonomousUpdateEdge) e);
        else
            System.exit(1);
    }
    
    @Override
    public Set<Edge> getEdge()
    {
        Set<Edge> ret = new HashSet<Edge>();
        ret.addAll(autonomousEdge);
        ret.addAll(controlledEdge);
        return ret;
    }
    
    public void setInvariant(Predicate p)
    {
        invariant = p;
    }
    
    public Predicate getInvariant()
    {
        return invariant;
    }
    
    @XmlElement
    public void setFlow(IntervalMatrix flow)
    {
        this.flow = flow;
    }
    
    public IntervalMatrix getFlow()
    {
        return flow;
    }
    
    public Set<AutonomousUpdateEdge> getAutonomousEdge()
    {
        if (this.autonomousEdge==null)
            this.autonomousEdge = new HashSet<AutonomousUpdateEdge>();
        return autonomousEdge;
    }
    
    public Set<ControlledUpdateEdge> getControlledEdge(int input)
    {
        Set<ControlledUpdateEdge> ret = new HashSet<ControlledUpdateEdge>();
        for (ControlledUpdateEdge cue: controlledEdge)
            if (cue.getGuard() == input)
                ret.add(cue);
        return ret;
    }
}
